//
//  AccountChangeController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/4/26.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "AccountChangeController.h"
#import "OrderPaymentCell.h"
#import "PayMoneyTextCell.h"
#import "DefaultButton.h"

@interface AccountChangeController ()<UITextFieldDelegate>
@property (nonatomic,strong)NSDictionary *userInfoDict;
@property(nonatomic,strong)MBProgressHUD *progressHUD;
@property(nonatomic,strong)UITextField *accountTextField;
@property(nonatomic,strong)UITextField *balanceTextField;
@property(nonatomic,strong)UITextField *payTextField;

@property(nonatomic,strong)UIButton *btn;

@end

@implementation AccountChangeController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"账户转换";
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.scrollEnabled = false;
    [self.tableView registerNib:[UINib nibWithNibName:@"PayMoneyTextCell" bundle:nil] forCellReuseIdentifier:@"AccountChangeID"];

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
    
    [self loadUserInfoNumberData];
}

- (void)loadUserInfoNumberData {
    
    NSDictionary *params = @{
                             @"username":USERNAME
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"api/User/info" completeBlock:^(id object) {
        self.userInfoDict = object;
        [self.tableView reloadData];
    }];
}

- (void)accountChangeDidButtonClick
{
    [self registerAllTextField];
    
//    NSLog(@"%@",self.accountTextField.text);
//    NSLog(@"%@",self.balanceTextField.text);
//    NSLog(@"%@",self.payTextField.text);
  
    
    if (!self.accountTextField.text.length || !self.payTextField.text.length) {
        self.progressHUD = [MBProgressHUD showMessage:@"请输入完整信息" toView:self.view dimBackground:false AndCompleteBlock:nil];
        [self.view addSubview:self.progressHUD];
        return;
    }
    
    //请求过程中禁止点击
    self.btn.enabled = false;
    [self.btn setBackgroundColor:[UIColor lightGrayColor]];

    NSDictionary *params = @{@"username":USERNAME,
                             @"amount":self.accountTextField.text,
                             @"code":self.payTextField.text
                             };
    NSLog(@"%@",params);
    @weakify(self);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/bankroll/index" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
         @strongify(self);
        if (JudgeBackDataCode) {
           
            self.progressHUD = [MBProgressHUD showMessage:@"转化成功" toView:self.view dimBackground:false AndCompleteBlock:^(id object){
                [self.navigationController popToRootViewControllerAnimated:true];
            }];
            [self.view addSubview:self.progressHUD];
            
        }else{
            
            NSDictionary *dict = [object objectForKey:@"message"];
            self.progressHUD = [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",dict[@"view"]] toView:self.view dimBackground:NO AndCompleteBlock:nil];
            [self.view addSubview:self.progressHUD];
            
        }
        
        self.btn.enabled = true;
        [self.btn setBackgroundColor:DEFAULT_COLOR];
    }];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else{
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dataDict = self.userInfoDict[@"data"];
    
    
    if (indexPath.section == 0) {
        
        static NSString *identifier=@"section0";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = BOLDFONT(15);
            cell.detailTextLabel.font = FONT(15);
        }
    
        if (indexPath.row == 0) {
            cell.textLabel.text = @"营业账户";
            cell.detailTextLabel.text = dataDict[@"yyye"];
        }else{
            cell.textLabel.text = @"充值账户";
            cell.detailTextLabel.text = dataDict[@"yue"];
        }

        return cell;
        
    }else{
        
        PayMoneyTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountChangeID" forIndexPath:indexPath];
        cell.PayTextField.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.PayTextField.keyboardType = UIKeyboardTypeDecimalPad;
        cell.PayTextField.tag = indexPath.row;
        cell.payLab.font = BOLDFONT(15);
        
        if (indexPath.row == 0) {
            cell.payLab.text = @"营业额转化充值账户";
            cell.PayTextField.placeholder = @"请填写金额";
            self.accountTextField = cell.PayTextField;
            [self.accountTextField becomeFirstResponder];
        }else if (indexPath.row == 1){
            cell.payLab.text = @"转化成的充值余额";
            cell.PayTextField.placeholder = @"0.00";
            self.balanceTextField = cell.PayTextField;
            self.balanceTextField.enabled = false;
            cell.PayTextField.borderStyle = UITextBorderStyleNone;
        }else{
            cell.payLab.text = @"请输入支付密码";
            cell.PayTextField.placeholder = @"支付密码";
            cell.PayTextField.secureTextEntry = true;
            self.payTextField = cell.PayTextField;
            cell.PayTextField.keyboardType = UIKeyboardTypeDefault;
        }
        return cell;
        
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 8;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.01;
    }else{
        return HEIGHT/6;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }else{
        return [self leaveView];
    }
}

- (UIView *)leaveView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT/6)];
    DefaultButton *button = [[DefaultButton alloc] init];
    self.btn = button;
    [button setTitle:@"提交信息" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(accountChangeDidButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.mas_centerX);
        make.centerY.equalTo(view.mas_centerY);
        make.height.mas_equalTo(view.Height/3);
        make.width.mas_equalTo(WIDTH-30);
    }];
    return view;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag == 0) {
        CGFloat account = [textField.text floatValue];
        CGFloat balance = account * 1;
        self.balanceTextField.text = [NSString stringWithFormat:@"%.2f",balance];
    }
}


- (void)registerAllTextField {
    [self.payTextField resignFirstResponder];
    [self.balanceTextField resignFirstResponder];
    [self.accountTextField resignFirstResponder];

}


@end
